import { GameObject } from '@/assets/GameObject.js'

export class PlayerParticle extends GameObject {
    constructor(gameMap, info) {
        super()
        this.gameMap = gameMap
        this.info = info
        this.friction = 0.9

        this.lifeTime = 0
        this.maxLifeTime = 1000
    }

    // 根据游戏地图大小重新调整粒子的大小和速度
    resize(widthRatio, heightRatio) {
        // 按比例缩放位置
        this.info.x *= widthRatio
        this.info.y *= heightRatio

        // 按比例缩放半径和其他属性
        this.info.radius *= heightRatio
        this.info.speed *= heightRatio
        this.info.moveLength *= heightRatio
    }

    start() {}

    // 更新玩家受伤粒子效果
    update_player_particle() {
        this.lifeTime += this.timeDelta

        if (this.lifeTime >= this.maxLifeTime) {
            this.destroy()
            return false
        }

        let moved = Math.min(this.info.moveLength, (this.info.speed * this.timeDelta) / 1000)
        this.info.x += this.info.vx * moved
        this.info.y += this.info.vy * moved
        this.info.speed *= this.friction
        this.info.moveLength -= moved
    }

    update() {
        this.update_player_particle()

        this.draw()
    }

    draw() {
        this.gameMap.ctx.beginPath()
        this.gameMap.ctx.arc(this.info.x, this.info.y, this.info.radius, 0, Math.PI * 2, false)
        this.gameMap.ctx.fillStyle = this.info.color
        this.gameMap.ctx.fill()
    }
}
